import request from '@/utils/request'

// 查询环保法规列表
export function listOpmEnvRule(query) {
  return request({
    url: '/environment/opmEnvRule/list',
    method: 'get',
    params: query
  })
}

// 查询环保法规详细
export function getOpmEnvRule(id) {
  return request({
    url: '/environment/opmEnvRule/' + id,
    method: 'get'
  })
}

// 新增环保法规
export function addOpmEnvRule(data) {
  return request({
    url: '/environment/opmEnvRule',
    method: 'post',
    data: data
  })
}

// 修改环保法规
export function updateOpmEnvRule(data) {
  return request({
    url: '/environment/opmEnvRule',
    method: 'put',
    data: data
  })
}

// 删除环保法规
export function delOpmEnvRule(id) {
  return request({
    url: '/environment/opmEnvRule/' + id,
    method: 'delete'
  })
}

export function getFile(query) {
  return request({
    url: '/system/file/download',
    method: 'get',
    params: query,
    responseType: 'blob'
  })
}

/*export function downloadFile(ossurl,targetName) {
  const fileName = ossurl.split(".")[2].substr(4)
  const type = ossurl.split(".")[3]
  console.log("ossurl:" + ossurl +",targetName: " + targetName);
  console.log("fileName:" + fileName +",type: " + type)
  getFile({
    fileName: fileName + "." + type
  }).then(res => {
    console.log(res.type);
    const blob = new Blob([res])
    let filename = targetName + "." + type
    if(typeof(targetName) == "undefined")
      filename = fileName + "." + type
    if ('download' in document.createElement('a')) { // 非IE下载
      const elink = document.createElement('a')
      elink.download = filename
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href) // 释放URL 对象
      document.body.removeChild(elink)
    } else { // IE10+下载
      navigator.msSaveBlob(blob, filename)
    }

  }).catch(
    error => {
      console.log(error)
    }
  )
}*/

export function downloadFile(ossurl,targetName) {
  const fileName = ossurl.split(".")[3].substr(4)
  const type = ossurl.split(".")[4]
  console.log("ossurl:" + ossurl +",targetName: " + targetName);
  console.log("fileName:" + fileName +",type: " + type)
  getFile({
    fileName: fileName +"."+type
  }).then(res => {
    console.log(res.type);
    const blob = new Blob([res])
    let filename = targetName + "." + type
    if(typeof(targetName) == "undefined")
      filename = fileName + "." + type
    if ('download' in document.createElement('a')) { // 非IE下载
      const elink = document.createElement('a')
      elink.download = filename
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href) // 释放URL 对象
      document.body.removeChild(elink)
    } else { // IE10+下载
      navigator.msSaveBlob(blob, filename)
    }

  }).catch(
    error => {
      console.log(error)
    }
  )
}
